<?php 
class ControllerReview extends Controller {
	function index() {  
		$config   =& $this->locator->get('config');
		$currency =& $this->locator->get('currency');
		$database =& $this->locator->get('database');
		$language =& $this->locator->get('language');
		$image    =& $this->locator->get('image');
		$module   =& $this->locator->get('module');
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$session  =& $this->locator->get('session');
		$tax      =& $this->locator->get('tax');
		$template =& $this->locator->get('template');
		$url      =& $this->locator->get('url');

		//pagination
        $session->set('review.page', $request->has('page')?(int)$request->get('page'):1);

		$language->load('controller/review.php');

    	$sql = "select r.review_id, r.author, r.rating, r.text, p.product_id, pd.name, p.price, i.filename, r.date_added from review r left join product p on(r.product_id = p.product_id) left join product_description pd on(p.product_id = pd.product_id) left join image i on(p.image_id = i.image_id) where p.product_id = '" . (int)$request->gethtml('product_id') . "' and p.date_available < now() and p.status = '1' and r.status = '1' and pd.language_id = '" . (int)$language->getId() . "' order by r.date_added desc";
    	$results = $database->getRows($database->splitQuery($sql, $session->get('review.page'), $config->get('config_max_rows')));

		$view = $this->locator->create('template');

		$view->set('tpl', $template);
		
		$view->set('text_results', $language->get('text_results', $database->getFrom(), $database->getTo(), $database->getTotal()));

      	$view->set('entry_page', $language->get('entry_page'));
		
		$view->set('page', $session->get('review.page'));
		
		$page_data = array();

      	for ($i = 1; $i <= $database->getPages(); $i++) {
				$query=array('path' => $request->gethtml('path'));
				if ($i >1) $query['page'] = $i;
        		$page_data[] = array(
          			'text'  => $language->get('text_pages', $i, $database->getPages()),
					'href'	=> $url->href('category', FALSE, $query),
          			'value' => $i
        		);
      	}

      	$view->set('pages', $page_data);
		
    	if ($results) {

			$product_info = $database->getRow("select * from product p left join product_description pd on p.product_id = pd.product_id where p.product_id = '" . (int)$request->gethtml('product_id') . "' and pd.language_id = '" . (int)$language->getId() . "'");
	  
	  		$template->set('title', $language->get('heading_title', $product_info['name']));

      		$view->set('heading_title', $language->get('heading_title', $product_info['name']));

      		$view->set('text_review_by', $language->get('text_review_by'));
      		$view->set('text_date_added', $language->get('text_date_added'));
      		$view->set('text_rating', $language->get('text_rating'));
      		
      		$view->set('price', $currency->format($tax->calculate($product_info['price'], $product_info['tax_class_id'], $config->get('config_tax'))));      		

      		$view->set('action', $url->href('review', FALSE, array('product_id' => $request->gethtml('product_id'))));

      		$review_data = array();

      		foreach ($results as $result) {
        		$review_data[] = array(
          			'href'       => $url->href('review_info', FALSE, array('review_id' => $result['review_id'])),
          			'name'       => $result['name'],
          			'thumb'      => $image->resize($result['filename'], $config->get('config_image_width'), $config->get('config_image_height')),
          			'text'       => trim(substr(strip_tags($result['text']), 0, 150)) . '...',
          			'rating'     => $result['rating'],
          			'out_of'     => $language->get('text_out_of', $result['rating']),
          			'author'     => $result['author'],
          			'date_added' => $language->formatDate($language->get('date_format_long'), strtotime($result['date_added']))
        		);
      		}

      		$view->set('reviews', $review_data);
      		
      		$view->set('button_write', $language->get('button_write'));
      		
      		$query = array(
        		'product_id' => $request->gethtml('product_id'),
        		'review_id'  => $request->gethtml('review_id')
      		); 

      		$view->set('write', $url->href('review_write', FALSE, $query));

	  		$template->set('content', $view->fetch('content/review.tpl'));
	  
	  		$template->set($module->fetch());
	
      		$response->set($template->fetch('layout.tpl'));   
    	} else {
      		$template->set('title', $language->get('text_empty'));
 
      		$view->set('heading_title', $language->get('text_empty'));

      		$view->set('text_error', $language->get('text_empty'));

      		$view->set('button_write', $language->get('button_write'));

      		$query = array(
        		'product_id' => $request->gethtml('product_id'),
        		'review_id'  => $request->gethtml('review_id')
      		); 

      		$view->set('write', $url->href('review_write', FALSE, $query));
      		
	  		$template->set('content', $view->fetch('content/review.tpl'));
	  
	  		$template->set($module->fetch());
	
      		$response->set($template->fetch('layout.tpl'));  
    	}
  	}
	
	function page() {
		$request  =& $this->locator->get('request');
		$response =& $this->locator->get('response');
		$session  =& $this->locator->get('session');
		$url      =& $this->locator->get('url');

		$query=array();

		$query['path'] = $request->gethtml('path');

		//backwards compat pagination
      	if ($request->has('page', 'post')) {
			$i=(int)$request->get('page', 'post');
			if ($i > 1) $query['page'] = $i;
      	}

		$response->redirect($url->href('review', FALSE, $query));
	}
}
?>
